package com.luck;

import com.luck.api.BlogClassManager;
import com.luck.api.BlogManager;
import com.luck.api.UserClassManager;
import com.luck.model.po.Blog;
import com.luck.model.po.BlogClass;
import com.luck.model.po.UserClass;
import com.luck.common.Constant;
import com.luck.model.dto.BlogInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by Hua wb on 2018/6/14.
 */
@Controller
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    BlogManager blogManager;
    @Autowired
    BlogClassManager blogClassManager;
    @Autowired
    UserClassManager userClassManager;

    @RequestMapping("/addBlogAction")
    public ModelAndView addBlogAction(HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView();
        int userId = getSessionUserId(httpSession);
        List<BlogClass> listBlogClass = blogClassManager.getListBlogClass();
        List<UserClass> listUserClass = userClassManager.getListUserClass(userId);
        modelAndView.setViewName("/blog/addBlog");
        modelAndView.addObject("listBlogClass", listBlogClass);
        modelAndView.addObject("listUserClass", listUserClass);
        return modelAndView;
    }
    @RequestMapping(value = "/addBlogInfo", produces = "text/html;charset=UTF-8")
    public String addBlogInfo(Blog blog, HttpSession httpSession){
        int userId = getSessionUserId(httpSession);
        blog.setUserId(userId);
        blog.setBlogKeyword("");
        String blogContent = blog.getBlogContent();
        if (blogContent.length() >= 450){
            blog.setBlogContent(blogContent.substring(0,450));
        }
        blog.setCreateDate(new Date());
        String blogName = blog.getBlogName();
        if (blogName.length() != 0 && blogContent.length() != 0){
            blogManager.addBlog(blog);
        }
        return Constant.REDIRECT_HOME;
    }

    private int getSessionUserId(HttpSession httpSession){
        Object userObject = httpSession.getAttribute(Constant.SESSION_USER_ID);
        int userId = 1;
        if (userObject != null) {
            userId = (int) userObject;
        }
        return userId;
    }

    @RequestMapping("/showBlog/{blogId}")
    public ModelAndView addBlogAction(@PathVariable("blogId") int blogId){
        ModelAndView modelAndView = new ModelAndView(Constant.REDIRECT_HOME);
        if (blogId == 0) {
            return modelAndView;
        }
        BlogInfoDto blogInfo = blogManager.getBlog(blogId);
        if (blogInfo == null) {
            return modelAndView;
        }
        modelAndView.addObject("blogInfo", blogInfo);
        modelAndView.setViewName("/blog/showBlog");
        return modelAndView;
    }
}
